import type { PageResult, PageParams } from '@/types/api.d'
import request from '@/utils/request'

/**
 * 医院基础信息
 */
export interface Hospital {
  id: number
  hospitalCode: string
  hospitalName: string
  delFlag: 'Y' | 'N'
  appID?: string
  appSecret?: string
  createTime: string
  createUser?: number
  updateTime: string
  updateUser?: number
}

/**
 * 医院搜索参数
 */
export interface HospitalSearchParams extends PageParams {
  hospitalName?: string
  hospitalCode?: string
  delFlag?: 'Y' | 'N'
}

/**
 * API响应类型
 */
interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

/**
 * 获取医院列表
 * @param params 搜索参数
 * @returns 分页医院列表
 */
export const fetchHospitals = (params: HospitalSearchParams): Promise<ApiResponse<PageResult<Hospital>>> => {
  return request.get('/hospitals', { params })
}

/**
 * 获取医院详情
 * @param id 医院ID
 * @returns 医院详情
 */
export const getHospital = (id: number): Promise<ApiResponse<Hospital>> => {
  return request.get(`/hospitals/${id}`)
}

/**
 * 创建医院
 * @param data 医院数据
 * @returns 创建后的医院信息
 */
export const createHospital = (data: Omit<Hospital, 'id'>): Promise<ApiResponse<Hospital>> => {
  return request.post('/hospitals', data)
}

/**
 * 更新医院
 * @param data 医院数据
 * @returns 更新后的医院信息
 */
export const updateHospital = (data: Hospital): Promise<ApiResponse<Hospital>> => {
  return request.put(`/hospitals/${data.id}`, data)
}

/**
 * 删除医院
 * @param id 医院ID
 * @returns 删除结果
 */
export const deleteHospital = (id: number): Promise<ApiResponse<void>> => {
  return request.delete(`/hospitals/${id}`)
}

/**
 * 切换医院状态
 * @param id 医院ID
 * @param delFlag 状态标记
 * @returns 切换结果
 */
export const toggleDelFlag = (id: number, delFlag: 'Y' | 'N'): Promise<ApiResponse<void>> => {
  return request.patch(`/hospitals/${id}/delFlag`, { delFlag })
}

/**
 * 医院API集合
 */
export const hospitalApi = {
  fetchHospitals,
  getHospital,
  createHospital,
  updateHospital,
  deleteHospital,
  toggleDelFlag
}
